﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Log
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009/10/29 18:13:03
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Log
{
    /// <summary>
    /// A simply logger
    /// </summary>
    public class SimplyLogger : ILog
    {
        private string m_name = "";

        public string Name { get { return m_name; } }

        public SimplyLogger() : this("",false) { }

        public SimplyLogger(string name) :this(name,false){ }

        public SimplyLogger(Type type) : this(type.Name, false) { }

        public SimplyLogger(Type type, bool isSaved) : this(type.Name, isSaved) { }

        public SimplyLogger(string name, bool isSaved)
        {
            this.m_name = name;
            if (isSaved)
            {
                System.Diagnostics.Trace.AutoFlush = true;
                System.Diagnostics.Trace.Listeners.Add(new TextWriterTraceListener(DateTime.Now.ToString("yyy-MM-dd")+".log"));
            }
        }
        private string GetName()
        {
            return m_name.Length == 0 ? "" : m_name;
        }
        private void WriteLine(object message, string prefix) 
        {
            System.Diagnostics.Trace.WriteLine(GetName()+" "+prefix+":" + message.ToString());
        }
        private void WriteLine(object message, string prefix, System.Exception exception)
        {
            System.Diagnostics.Trace.WriteLine(GetName() + prefix + ":" + message.ToString() + "\n\r Exception " + exception.StackTrace);
        }
        #region ILog 成员

        public void Debug(object message)
        {
#if DEBUG
            WriteLine(message,"Debug");
#endif
            }

        public void Debug(object message, System.Exception exception)
        {
#if DEBUG
            WriteLine(message, "Debug", exception);
#endif
        }

        public void Error(object message)
        {
            WriteLine(message, "Error");
        }

        public void Error(object message, System.Exception exception)
        {
            WriteLine(message, "Error", exception);
        }

        public void Fatal(object message)
        {
            WriteLine(message, "Fatal");
        }

        public void Fatal(object message, System.Exception exception)
        {
            WriteLine(message, "Fatal", exception);
        }

        public void Info(object message)
        {
            WriteLine(message, "Info");
        }

        public void Info(object message, System.Exception exception)
        {
            WriteLine(message, "Info", exception);
        }

        public void Trace(object message)
        {
            WriteLine(message, "Trace");
        }

        public void Trace(object message, System.Exception exception)
        {
            WriteLine(message, "Trace", exception);
        }

        public void Warn(object message)
        {
            WriteLine(message, "Warn");
        }

        public void Warn(object message, System.Exception exception)
        {
            WriteLine(message, "Warn", exception);
        }

        public void Debug(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Debug");
        }

        public void Error(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Error");
        }

        public void Fatal(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Fatal");
        }

        public void Info(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Info");
        }

        public void Trace(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Trace");
        }

        public void Warn(string message, params object[] args)
        {
            WriteLine(string.Format(message.ToString(), args), "Warn");
        }

        #endregion
    }
}
